<?php
/*
// author: @sunguide 
// time:   @2013-10-17 15:36:05
// description: 考勤管理中心
*/
namespace Console\Controller;
use Console\Model\AuthGroupModel;
use COM\Page;

class AttendenceController  extends \Console\Controller\ConsoleController { 
    /* 保存允许访问的公共方法 */
	static protected $allow = array( 'index');

    /* 左侧节点菜单定义 */
    static protected $nodes =   array(
        
		array('title'=>'在线打卡', 'url'=>'Attendence/recordManage', 'group'=>'考勤管理'),
		array('title'=>'请假申请', 'url'=>'Attendence/leave', 'group'=>'考勤管理'),
		array('title'=>'请假审批', 'url'=>'Attendence/audit', 'group'=>'考勤管理',),
		array('title'=>'考勤管理', 'url'=>'Attendence/index', 'group'=>'考勤管理',),
    );
	public function index(){
		$Member = M("Member")->alias('A')->join('onethink_position B ON A.position_id = B.id')->join('onethink_department C ON A.department_id = C.id');
        $MA = M('MemberAttendence');
		//$list   = $this->lists($Member);
		$list = $this->lists ($Member,$where=array('A.status'=>array('egt',1)),$order='A.uid DESC',$base = array(),$field='A.*,B.title position,C.title department');
        int_to_string($list,array('A.status'=>array(1=>'在职',-1=>'离职',0=>'待入职',2=>'未审核',3=>'草稿')));
		//dump($Member);
		//echo $Member->getLastSql();
		$date = mktime(0,0,0,date('m'),date('d'),date('Y'));
		foreach($list as $k=>$v){
			$v['position_id'] ? 1:$list[$k]['position']="暂无职位";
			$v['department_id'] ? 1:$list[$k]['department']="暂无部门";
			$r = $MA->where(array('uid'=>$v['uid'],'attendence_date'=>$date,'type'=>0))->find(); //打卡记录
			$list[$k]['attendence_time'] = $r['attendence_time'];
			$list[$k]['absence_time'] = $r['absence_time'];
		}
		//dump($list);
		$this->assign('currentGroup',$this->getMenuCurrent(self::$nodes));
		$this->assign('meta_title', '请假申请');
		$this->assign('list', $list);
		$this->display();
	}
	// 
	public function recordManage(){
		$Member = M("Member")->alias('A')->join('onethink_position B ON A.position_id = B.id')->join('onethink_department C ON A.department_id = C.id');
        $MA = M('MemberAttendence');
		//$list   = $this->lists($Member);
		$list = $this->lists ($Member,$where=array('A.status'=>array('egt',1)),$order='A.uid DESC',$base = array(),$field='A.*,B.title position,C.title department');
        int_to_string($list,array('A.status'=>array(1=>'在职',-1=>'离职',0=>'待入职',2=>'未审核',3=>'草稿')));
		//dump($Member);
		//echo $Member->getLastSql();
		$date = mktime(0,0,0,date('m'),date('d'),date('Y'));
		foreach($list as $k=>$v){
			$v['position_id'] ? 1:$list[$k]['position']="暂无职位";
			$v['department_id'] ? 1:$list[$k]['department']="暂无部门";
			$r = $MA->where(array('uid'=>$v['uid'],'attendence_date'=>$date,'type'=>0))->find(); //打卡记录
			$list[$k]['attendence_time'] = $r['attendence_time'];
			$list[$k]['absence_time'] = $r['absence_time'];
		}
		//dump($list);
		$this->assign('currentGroup',$this->getMenuCurrent(self::$nodes));
		$this->assign('meta_title', '请假申请');
		$this->assign('list', $list);
		$this->display('record');
	}
	//签到或者签退
	public function makeRecord(){
		$MA = M('MemberAttendence');
		$uid = I('get.uid');
		if($uid){
			$date = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$map=array(
				'uid' => $uid,
				'attendence_date' =>$date,
				'type' => 0 //上班
			);
			$type=I('get.type');
			$data = $map;
			switch($type){
				case '1'://签到
				    $data['attendence_time'] = time();
				    break;
				case '2'://签退
				    $data['absence_time'] = time();
				    break;
			}
			if($MA->where($map)->find()){
				$r = $MA->where($map)->save($data);
			}else{
				$r = $MA->add($data);
			}
			if($r){
				$this->success('成功');
			}else{
				$this->error('也许你操作太快了,导致失败');
			}
		}
	}
    public function leave(){
		if(IS_POST){
			$MA = M('MemberAttendence');
			$data = $MA->create();
			$date = mktime(0,0,0,date('m'),date('d'),date('Y'));
			//判断是否存在已经申请的时间段中
			$MA->absence_time = strtotime($MA->absence_time); 
			$MA->attendence_time = strtotime($MA->attendence_time);
			if($MA->absence_time >= $MA->attendence_time) $this->error('请假开始日期，不能晚于结束日期');
			if(!$MA->reason) $this->error('请假理由必须');
			$map=array(
				'uid' => UID,
				'absence_time' => array('egt' => $MA->absence_time), 
				'absence_time' => array('elt' => $MA->absence_time), 
				'type' => array('gt' => 0)
			);
			if($MA->where($map)->find()){
				$this->error('已经存在该时间段中的申请，如需修改，请删除原来的申请。');
			}else{
				$MA->uid = UID;
				$MA->attendence_date =$date;
				$r = $MA->add();
			}
			if($r){
				$this->success('申请提交成功');
			}else{
				$this->error('申请提交失败');
			}
			
			
		}else{
			$this->assign('currentGroup',$this->getMenuCurrent(self::$nodes));
			$this->assign('meta_title', '请假申请');
			$this->assign('main_title', '请假申请');
			$this->display('edit');
		}
		
	}
	public function audit(){
		$id = I('id');
		if($id){
			if(I('get.method') == 'delete'){
				if(is_array($id)){
					foreach($id as $k => $i){
						$v = true;
						M("MemberAttendence")->where("id = ".$i)->delete() ? 1 : $v = true;
					}
				}else{
					$v = M("MemberAttendence")->where("id = ".$id)->delete();
				}
				if($v){
					$this->success('操作成功');
				}else{
					$this->error('操作失败');
				}
			
			}else{
				$data = array('status'=>I('get.status'));
				if(is_array($id)){
					foreach($id as $k => $i){
						$v = true;
						M("MemberAttendence")->where("id = ".$i)->save($data) ? 1 : $v = true;
					}
				}else{
					$v = M("MemberAttendence")->where("id = ".$id)->save($data);
				}
				if($v){
					$this->success('操作成功');
				}else{
					$this->error('操作失败');
				}
			}
		}else{
			$MemberAttendence = M("MemberAttendence");
			$list = $this->lists ($MemberAttendence,$where=array('type'=>array('gt',0)),$order='id DESC',$base = array(),$field='');
			int_to_string($list,array('status'=>array(1=>'在职',-1=>'离职',0=>'待入职',2=>'未审核',3=>'草稿')));
			//dump($Member);
			//echo $MemberAttendence->getLastSql();
			foreach($list as $k=>$v){
				$list[$k]['approve'] = U("Attendence/audit?status=1&id=".$v['id']);
				$list[$k]['oppose'] = U("Attendence/audit?status=-1&id=".$v['id']);
			}
			//dump($MemberAttendence);
			$this->assign('currentGroup',$this->getMenuCurrent(self::$nodes));
			$this->assign('meta_title', '请假申请');
			$this->assign('main_title', '请假审批');
			$this->assign('list', $list);
			$this->display('audit');

		}
	}
	//获取时间戳
	public function getTimestrap(){
		$this->success(time());
	}
}
